#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/wait.h>

int mysys(const char *command)
{
	char *com_copy = malloc(strlen(command));
	strcpy(com_copy,command);
	char *argv[16] = {NULL};
	int i = 0;
	char *word = strtok(com_copy," ");
	while(word != NULL)
	{
		argv[i] = (char *)malloc(strlen(word));
		strcpy(argv[i],word);
		word = strtok(NULL," ");
		i+=1;
	}
	argv[i] = NULL;
	
	
	pid_t pid = fork();
	if(argv[0] == NULL)
	{
		return 1;
	}
	if(pid < 0)
	{
		return -1;
	}
	else if(pid == 0)
	{
		int error = execvp(argv[0],argv);
		return 127;
	}
	else
	{
		int status;
		wait(&status);
		if(!(WIFEXITED(status))) return -1;
	}
	return 0;
}

int main()
{
	printf("-----------------------------\n");
	mysys("echo HELLO WORLD");
	printf("-----------------------------\n");
	mysys("ls /");
	printf("-----------------------------\n");
	return 0;
}
